import { Component, OnInit } from '@angular/core';
import { Params, ActivatedRoute } from '@angular/router';
import { AuthService } from 'src/app/services/auth.service';
import * as constants from '../../../providers/Constants'
import { ToastService } from './../../../providers/toast/toast.service'
import { NavController } from '@ionic/angular';
import { Storage } from 'src/app/providers/Storage';

@Component({
  selector: 'app-address',
  templateUrl: './address.page.html',
  styleUrls: ['./address.page.scss'],
})
export class AddressPage implements OnInit {
  noAddress:boolean=false;
  addressList: any = [];
  isSelect: boolean = false;
  constructor(private activeRoute: ActivatedRoute, private nav: NavController, private auth: AuthService, private toast: ToastService) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      if (params.type) {
        console.log('params.goodsInfo=', params.type);
        this.isSelect = true;
      }else{
        Storage.sessionStorage.remove('addressItem')
      }
    })
  }
  ionViewDidEnter() {
    this.getData();
  }
  ngOnInit() {
  }
  onClickItem(item,event) {
    if ( this.isSelect) {
      console.log('--------------------------------------');
      
      // Storage.sessionStorage.set('addressType', '1');
      Storage.sessionStorage.set('addressItem', item);
      this.nav.pop();
      event.stopPropagation();//阻止冒泡
      event.preventDefault();// 阻止默认行为 
    }
  }
  add() {
    this.nav.navigateForward('address-edit', {
      queryParams: {
        edit: 0,
      }
    });
  }
  edit(item,event) {
    this.nav.navigateForward('address-edit', {
      queryParams: {
        edit: 1,
        addressItem: JSON.stringify(item)
      }
    });
    event.stopPropagation();//阻止冒泡
    event.preventDefault();// 阻止默认行为 
  }
  getData() {
    this.auth.queryAddress({}).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        this.noAddress = false;
        this.addressList = res.data;
      } else if (res.retCode == '000025') {
        this.addressList = [];
        this.noAddress = true;
      } else if (constants.errCode[res.retCode]) {
        this.noAddress = true;
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.noAddress = true;
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }
}
